<?php ob_start();
session_start(); // on relaye la session 
if(isset($_SESSION["is_registered"]) && $_SESSION["is_registered"] == "registered"){
		$enregistrer = $_SESSION["is_registered"];
}else{
	header("location:../../../menu.php"); // redirection en cas d'echec
}
require_once('../../../Connections/microassur.php');
require_once('../../../utils/fonctions.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1"))
$bool=verifLibelle1('Code_evenement') && verifLibelle('Libelle_evenement') && verifLibelle('Identifiant_profil');

// --- Verifie si le code existe pour le profil
$toCriterias['Code_evenement'] = (array_key_exists('Code_evenement', $_POST)) ? $_POST['Code_evenement'] : '' ;
$toCriterias['Identifiant_profil'] = (array_key_exists('Identifiant_profil', $_POST)) ? $_POST['Identifiant_profil'] : 0 ;
$nums = existeEnregistrement($database_microassur, $microassur,"habilitation",$toCriterias) ;

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1") &&  $bool && $nums < 1) {
  $insertSQL = sprintf("INSERT INTO habilitation (Identifiant_habilitation, Code_evenement, Libelle_evenement, Identifiant_profil) VALUES (%s, %s, %s, %s)",
                       GetSQLValueString($_POST['Identifiant_habilitation'], "int"),
                       GetSQLValueString($_POST['Code_evenement'], "text"),
                       GetSQLValueString($_POST['Libelle_evenement'], "text"),
                       GetSQLValueString($_POST['Identifiant_profil'], "int"));

  mysql_select_db($database_microassur, $microassur);
   mysql_query("START TRANSACTION");
  $Result1 = mysql_query($insertSQL, $microassur) or die(mysql_error());

  if($Result1){ mysql_query("commit");header("location:index.php?ajout=success");}

else {mysql_query("rollback");$message = colorer("Contacter la DSI, erreur création habilitation","false");}
}
elseif(isset($_POST["MM_insert"])&& !$bool) {

if(empty($_POST['Code_evenement'])) $message = colorer("Vous devez saisir le code évènement","false");
elseif(!verifLibelle1('Code_evenement')) $message = colorer("Le code évènement saisi comporte des caractères spéciaux interdits","false");
elseif(empty($_POST['Libelle_evenement'])) $message = colorer("Vous devez saisir le libellé évènement","false");
elseif(!verifLibelle('Libelle_evenement')) $message = colorer("Le libellé évènement saisi comporte des caractères spéciaux interdits","false");

elseif(empty($_POST['Identifiant_profil'])) $message = colorer("L'habilitation n'est pas rattachée à un profil,vous devez saisir un profil","false");
else
//$message=colorer("Erreur: Au moins un champ est vide ou valeur incorrecte","false");
$message="";

}
elseif(isset($_POST["MM_insert"]) && $nums)
{
    $message = colorer("Cette habilitation existe déjà pour ce profil","false");
}

/*if  ( isset($_POST["MM_insert"]) )
{
    $toCriterias['Code_evenement'] = $_POST['Code_evenement'] ;
    $toCriterias['Identifiant_profil'] = $_POST['Identifiant_profil'] ;
    $nums=existeEnregistrement($database_microassur, $microassur,"habilitation",$toCriterias);
    if ($nums >= 1)
    {
        $message = colorer("Cette habilitation existe déjà pour ce profil","false") ;
    }
    
}*/
    

mysql_select_db($database_microassur, $microassur);
$query_rs = "SELECT * FROM habilitation ORDER BY Code_evenement ASC";
$rs = mysql_query($query_rs, $microassur) or die(mysql_error());
$row_rs = mysql_fetch_assoc($rs);
$totalRows_rs = mysql_num_rows($rs);
mysql_select_db($database_microassur, $microassur);
$query_rs_profil = "SELECT * FROM profil ORDER BY Libelle_profil ASC";
$rs_profil = mysql_query($query_rs_profil, $microassur) or die(mysql_error());
$row_rs_profil = mysql_fetch_assoc($rs_profil);
$totalRows_rs_profil = mysql_num_rows($rs_profil);
?>
<?php  if(isset($_GET['ajout']) && $_GET['ajout']=='success') $message = colorer("L'habilitation est créée","true"); ?>
<?php  if(isset($_GET['supp']) && $_GET['supp']=='err') $message = colorer("Contacter la DSI, erreur suppression habilitation","false"); ?>
<?php if(isset($_GET['mod']) && $_GET['mod']=='success') $message = colorer("La modification de l'habilitation est prise en compte","true"); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=8" />
			<title><?php echo $titleGen ?></title>
			<link rel="stylesheet" type="text/css" href="../../../css/main.css" />
			<link rel="stylesheet" type="text/css" href="../hab.css.css" />
			<!--[if IE 7]><link rel="stylesheet" type="text/css" href="../../../css/ie7.css" /><![endif]-->
	</head>
<body>
			<div id="sf_menu1">
				<h3>Administration</h3>
				<?php
					if(isset($message))
						echo $message;
					$nommodule="Habilitation";
					$nommodule2="Liste des habilitations";
					$nom_module_saisie="Ecran saisie";
					?>
			</div>
			<div class="cleared"></div>
		<div class="corps1">
			<!--/***************************************/-->
			<div class="imgleft"></div>
			<div class="imgmiddle">
			<div class="btn"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule; ?></h5></span><span class="btnright"></span></div>
			<div class="btn1"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule2; ?></h5></span><span class="btnright"></span></div>
			<div class="cleared"></div>
			<table class="tab_list ns0">
						<tr id="niv1">
							<td class="t2">CODE EVENEMENT</td>
							<td class="t2">LIBELLE EVENEMENT</td>
							<td>PROFIL</td>
						  </tr>
			</table>
				<div class="content_table">
					<table class="tab_list niv2">
						  <?php $i=1; do 	{
						  $colname1_rs_profil_liste = "-1";
						if (isset($row_rs['Identifiant_profil'])) {
						  $colname1_rs_profil_liste = $row_rs['Identifiant_profil'];
						}
						mysql_select_db($database_microassur, $microassur);
						$query_rs_profil_liste = sprintf("SELECT * FROM profil WHERE Identifiant_profil = %s", GetSQLValueString($colname1_rs_profil_liste, "text"));
						$rs_profil_liste = mysql_query($query_rs_profil_liste, $microassur) or die(mysql_error());
						$row_rs_profil_liste = mysql_fetch_assoc($rs_profil_liste);
						$totalRows_rs_profil_liste = mysql_num_rows($rs_profil_liste);

						  ?>
						  <?php if ($totalRows_rs > 0) { // Show if recordset not empty ?>
							<tr <?php if($i%2==0) echo 'class="colorligne"'; ?> >
							  <td class="t2"><a href="modhabilitation.php?Identifiant_habilitation=<?php echo $row_rs['Identifiant_habilitation']; ?>"><?php echo $row_rs['Code_evenement']; ?></a></td>
							  <td class="t2"><a href="modhabilitation.php?Identifiant_habilitation=<?php echo $row_rs['Identifiant_habilitation']; ?>"><?php echo $row_rs['Libelle_evenement']; ?></a></td>
							  <td><a href="modhabilitation.php?Identifiant_habilitation=<?php echo $row_rs['Identifiant_habilitation']; ?>"><?php echo $row_rs_profil_liste['Libelle_profil']; ?></a></td>
							</tr>
							<?php } // Show if recordset not empty ?>

						  <?php $i++; } while ($row_rs = mysql_fetch_assoc($rs)); ?>
											</table>
										</div>
									</div>
									<div class="imgright"></div>
								<div class="cleared"></div>
								<!--/***************************************/-->
								<div id="br"></div>
		<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1" class="hab1">
			<div class="imgleft"></div>
				<div class="imgmiddle">
					<div class="btn"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule; ?></h5></span><span class="btnright"></span></div>
					<div class="btn1"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nom_module_saisie; ?></h5></span><span class="btnright"></span></div>
						<div class="content_tableform">
						  <table>
							<tr>
							  <td>CODE EVENEMENT</td>
							  <td><input class="resize_1" type="text" maxlength="5" name="Code_evenement" value="<?php if(isset($_POST['Code_evenement']))echo remind_text('Code_evenement');?>" size="32" /></td>
							</tr>
							<tr>
							  <td>LIBELLE EVENEMENT</td>
							  <td><input name="Libelle_evenement" type="text"  maxlength="30" value="<?php if(isset($_POST['Libelle_evenement']))echo remind_text('Libelle_evenement');?>" size="40" />
							  </td>
							</tr>
							<tr>
							  <td>PROFIL</td>
							  <td><select name="Identifiant_profil">
								<?php
						do {  
						?>
								<option value="<?php echo $row_rs_profil['Identifiant_profil']?>"><?php echo $row_rs_profil['Libelle_profil']?></option>
								<?php
						} while ($row_rs_profil = mysql_fetch_assoc($rs_profil));
						  $rows = mysql_num_rows($rs_profil);
						  if($rows > 0) {
							  mysql_data_seek($rs_profil, 0);
							  $row_rs_profil = mysql_fetch_assoc($rs_profil);
						  }
						?>
							  </select>
							  </td>
							</tr>
							  </table>
							</div>
					</div>
					<div class="imgright"></div>				
						<div class="cleared"></div>
						
							<div class="bt1"><a href="<?php echo $retour; ?>"><span class="btleft"></span><span class="btmiddle">Retour</span><span class="btright"></span></a></div>
							<div class="bt"><span class="btleft"></span><input type="submit" name="Creer" value="Créer" class="resizeall btmiddle"/><span class="btright"></span></div>
							
						<div class="cleared"></div>
			  <input type="hidden" name="Identifiant_habilitation" value="" />
			  <input type="hidden" name="MM_insert" value="form1" />
			</form>
			</div>
</body>
</html><?php ob_end_flush();?>
<?php
mysql_free_result($rs);
mysql_free_result($rs_profil);
mysql_free_result($rs_profil_liste);
?>
